﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@Html.Partial("_ProductInfo_CreateOrEdit")


@Html.Partial("_ProductVariant_List")


@Html.Partial("_ProductPicture_List")


@Html.Partial("_ProductSpecificationAttribute_List")

<input type="button" value="Create" data-bind="click:SaveProductInfo"/>



@{
    var model = ViewBag.Model;
    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}


@section scripts
{
    

<script>
    var init = @jsonStr;

   
    function ProductSpecificationAttribute(specificationAttributeName, optionName) {

        var self = this;
        self.SpecificationAttributeName = ko.observable(specificationAttributeName);
        self.OptionName = ko.observable(optionName);
    }
    
    function ProductPictureModel(pictureId, pictureUrl,displayOrder) {
        this.PictureId = ko.observable(pictureId);
        this.DisplayOrder = ko.observable(displayOrder);
        this.PictureUrl = ko.observable(pictureUrl);
    }

    function ProductVariantModel(id, name,sku,price, stockQuantity,displayOrder,published) {

        this.Id = ko.observable(id);
        this.Name= ko.observable(name);
        this.Sku= ko.observable(sku);
        this.Price= ko.observable(price);
        this.StockQuantity= ko.observable(stockQuantity);
        this.DisplayOrder= ko.observable(displayOrder);
        this.Published = ko.observable(published);
    }
    
    function ProductModel() {
        var self = this;
        self.Id = ko.observable(init.Id);
        self.ProductName = ko.observable(init.ProductName);
        self.ShortDescription= ko.observable(init.ShortDescription);
        self.FullDescription= ko.observable(init.FullDescription);
        self.IsPublished = ko.observable(init.Published);

        var tempProductPictures= $.isEmptyObject(init.ProductPictures)? []: $.map(init.ProductPictures, function (a) {
            return new ProductPictureModel(a.Id, a.PictureUrl, a.Name);
        });

        var tempProductVariants=  $.isEmptyObject(init.ProductVariants)? []:$.map(init.ProductVariants, function (a) {
            return new ProductVariantModel(a.Id, a.Name, a.Sku, a.Price, a.StockQuantity, a.DisplayOrder,a.Published);
        });

        var tempProductSpecificationAttributes=  $.isEmptyObject(init.ProductSpecificationAttributes)? []:$.map(init.ProductSpecificationAttributes, function (a) {
            return new ProductSpecificationAttribute(a.SpecificationAttributeName, a.SpecificationAttributeOptionName);
        });

       
        self.ProductVariants = ko.observableArray(tempProductVariants);
        self.ProductPictures = ko.observableArray(tempProductPictures);
        self.ProductSpecificationAttributes = ko.observableArray(tempProductSpecificationAttributes);
@*        
        self.TempPicture = new ProductPictureModel(0, '@Url.Content("~/Images/default-image_100.gif")', 0);*@

        self.DeleteProductPicture = function() {
            $.ajax({
                url: "/api/ProductPictureApi/DeleteProductPicture",
                type: 'post',
                data: ko.toJSON(self),
                contentType: 'application/json',
                success: function (result) {

                    if (result.Status == "0") {
                        alert("保存成功");
                    }
                    else {
                        alert("保存失败：" + result.Msg);
                    }
                }
            });
        };

        
        self.SaveProductInfo = function() {
            $.ajax({
                url: "/api/ProductApi/UpdateProductInfo",
                type: 'post',
                data: ko.toJSON(self),
                contentType: 'application/json',
                success: function (result) {

                    if (result.Status == "0") {
                        alert("保存成功");
                    }
                    else {
                        alert("保存失败：" + result.Msg);
                    }
                }
            });
        };

    }

    var vm = new ProductModel();
    ko.applyBindings(vm);



</script>
}
